package com.boomsecret.demo;

//import com.sun.java.accessibility.AccessBridge;
import org.junit.Test;

import java.lang.reflect.Method;

/**
 * Created by wangjinliang on 2018/1/12.
 */
public class ClassLoaderDemo {

    @Test
    public void testBootstrapClassLoader() {
        ClassLoader classLoader = String.class.getClassLoader();
        try {
            Class<?> loadClass = classLoader.loadClass("com.boomsecret.domain.Person");
            Object obj = loadClass.newInstance();
            Method method = loadClass.getMethod("setUsername", String.class);
            method.invoke(obj, "Bootstrap ClassLoader");
            Method getUsernameMethod = loadClass.getMethod("getUsername");
            Object invoke = getUsernameMethod.invoke(obj);
            System.out.println(invoke);
            System.out.println(obj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

//    @Test
//    public void testExtensionClassLoader() {
//        ClassLoader classLoader = AccessBridge.class.getClassLoader();
//        try {
//            Class<?> loadClass = classLoader.loadClass("com.boomsecret.domain.Person");
//            Object obj = loadClass.newInstance();
//            Method method = loadClass.getMethod("setUsername", String.class);
//            method.invoke(obj, "Extension ClassLoader");
//            Method getUsernameMethod = loadClass.getMethod("getUsername");
//            Object invoke = getUsernameMethod.invoke(obj);
//            System.out.println(invoke);
//            System.out.println(obj);
//        } catch (Exception e) {
//            System.out.println(e);
//        }
//    }


    @Test
    public void testApplicationClassLoader() {
        ClassLoader classLoader = getClass().getClassLoader();
        try {
            Class<?> loadClass = classLoader.loadClass("com.boomsecret.domain.Person");
            Object obj = loadClass.newInstance();
            Method method = loadClass.getMethod("setUsername", String.class);
            method.invoke(obj, "Application ClassLoader");
            Method getUsernameMethod = loadClass.getMethod("getUsername");
            Object invoke = getUsernameMethod.invoke(obj);
            System.out.println(invoke);
            System.out.println(obj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Test
    public void testLoadClass() {
        try {
            Class<?> loadClass = Class.forName("com.boomsecret.domain.Person");
            Object obj = loadClass.newInstance();
            Method method = loadClass.getMethod("setUsername", String.class);
            method.invoke(obj, "Application ClassLoader");
            Method getUsernameMethod = loadClass.getMethod("getUsername");
            Object invoke = getUsernameMethod.invoke(obj);
            System.out.println(invoke);
            System.out.println(obj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Test
    public void testLoadClass2() {
        try {
            Class<?> loadClass = Class.forName("com.boomsecret.domain.Person",
                    true, getClass().getClassLoader());
            Object obj = loadClass.newInstance();
            Method method = loadClass.getMethod("setUsername", String.class);
            method.invoke(obj, "Application ClassLoader");
            Method getUsernameMethod = loadClass.getMethod("getUsername");
            Object invoke = getUsernameMethod.invoke(obj);
            System.out.println(invoke);
            System.out.println(obj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
